<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use App\Http\model\Power;

class PowerController extends Controller
{
	/**
	 * [addPower 添加权限]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @version  6.2
	 */
	public function addPower(Request $request){

		$result = array();

    	if ($request['name'] && $request['number']&& $request['pid']  && $request['url'] && $request['sort'] ) {
    		
    		$powerArr = $request->all();
            if (isset($powerArr['s'])) {
                $powerArr = array_except($powerArr, ['s']);
            }
    		$power = new Power;

    		$result = $power->addPowerModel($powerArr);

    		return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);

    	}
    }

    /**
     * [findPower 权限列表]
     * @Author   孙丽男
     * @DateTime 2018-06-05
     * @version  6.2
     * @return   [json]     [$result]
     */
    public function findPower(Request $request){

    	//构建查询条件
        $where = array();

        if ($request['powerId']) {
            
            $where = [
                ['power.id', $request['powerId']],
            ]; 
        }

        if ($request['name']) {
            
            $where[1] = ['power.name', 'like', '%' . $request['name'] . '%'];
        }

        if ($request['start_time']) {
            
            $where[2] =  ['power.update_time', '>',  $request['start_time']];
        }

        if ($request['end_time']) {
            
            $where[3] =  ['power.update_time', '<',  $request['end_time']];
        }
        $where[4] =  ['power.pid', '<>',  0];
        
        $power = new Power;

        $result = $power->findPowerModel($where);

        return $result;

    }

    /**
     * [deletePower 删除权限]
     * @Author   孙丽男
     * @DateTime 2018-06-05
     * @param    [权限id] array $powerId[]
     * @version  6.2
     * @return   [json]   $result
     */
    public function deletePower(Request $request){

        $result = array();
   
        if (is_array($request['powerId'])) {

            $powerArr = $request->all();
            if (isset($powerArr['s'])) {
                $powerArr = array_except($powerArr, ['s']);
            }

            $powerIdArr = $powerArr['powerId'];

            $power = new Power;

            $result = $power->deletePowerModel($powerArr);

            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }

    /**
     * [updatePower 修改权限]
     * @Author   孙丽男
     * @DateTime 2018-06-05
     * @param    [权限id]  int $powerId
     * @param    [修改的信息]  ....
     * @version  6.2
     * @return   [json]   $result
     */
    public function updatePower(Request $request){


        if ($request['powerId']) {
            $powerId = $request['powerId'];
            $updateInfo = $request->all();
            if (isset($updateInfo['s'])) {
                $updateInfo = array_except($updateInfo, ['s']);
            }
            unset($updateInfo['powerId']);

            if ($updateInfo) {
                
                $power = new Power;
                $result = $power->updatePowerModel($powerId, $updateInfo);
                return $result;

            }else{

                $result['code'] = 1002;
                $result['msg'] = '请填写修改数据';
                return json_encode($result);
            }

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }

     /**
     * [updateUser 修改权限状态]
     * @Author   孙丽男
     * @DateTime 2018-06-04
     * @param    [权限id组]  array $powerId[]
     * @param    [状态]  int  $status
     * @version  6.2
     * @return   [json]     [$result]
     */    
    public function updateStatus(Request $request){


        if (is_array( $request['powerId']) && $request['status']) {
            $powerId = $request['powerId'];
            $power = new Power;
            $result = $power->updateStatusModel($powerId, $request['status']);
            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }
}
